import Vue from 'vue'
import Echarts from '../assets/js/echarts.min.js'

export default {
    bind: function (el, binding) {
        console.log('bind')
        //console.info("el: %o", el)
        const options = binding.value

        Vue.nextTick(function () {
            //console.info("bind-nextTick")
            el.__echarts__ = Echarts.init(el)
            el.__echarts__.setOption(options)
            // auto resize
            el.__onresize__ = function () {
                el.__echarts__.resize()
            }

            if (window.attachEvent) {
                window.attachEvent('onresize', el.__onresize__)
            } else {
                window.addEventListener('resize', el.__onresize__, false)
            }
        })
    },
    update: function (el, binding) {
        //console.info('update')
        const options = binding.value
        //console.info("binding.value: %o", binding.value)

        Vue.nextTick(function () {
            //console.info('update-nextTick')
            el.__echarts__.clear()
            el.__echarts__.setOption(options)
        })
    },
    unbind: function (el) {
        //console.info('unbind')
        el.__echarts__.dispose()

        if (window.attachEvent) {
            window.detachEvent('onresize', el.__onresize__);
        } else {
            window.removeEventListener('resize', el.__onresize__, false);;
        }
    }
}